Ext.define('Noter.view.NoteGrid',{
    extend: 'Ext.grid.Panel',
    requires: [ 
            //'Ext.data.TreeStore',
            'Noter.stores.Notes',
    ],
    alias: 'widget.notegrid',

                height: 300,
                iconCls: 'icon-user',
                columns: [{
                    text: 'ID',
                    width: 40,
                    sortable: true,
                    dataIndex: 'id'
                }, {
                    text: 'Name',
                    flex: 1,
                    sortable: true,
                    dataIndex: 'ntname',
                    field: {
                        xtype: 'textfield'
                    }
                }, {
                    header: 'Content',
                    sortable: true,
                    dataIndex: 'content',
                    field: {
                        xtype: 'textfield'
                    }
                }, {
                    header: 'Pid',
                    width: 80,
                    sortable: true,
                    dataIndex: 'pid',
                    field: {
                        xtype: 'textfield'
                    }
                }],
                bbar: Ext.create('Ext.PagingToolbar', {
             //       store: store,
                    displayInfo: true,
                    //displayMsg: 'Displaying topics {0} - {1} of {2}',
                    //emptyMsg: "No topics to display",
                }),
                dockedItems: [{
                    xtype: 'toolbar',
                    items: [{
                        text: 'Add',
                        iconCls: 'add',
                        handler: function(){
                            store.insert(0, new Noter.models.Note());
                            rowEditing.startEdit(0, 0);
                        }
                    }, '-', {
                        itemId: 'delete',
                        text: 'Delete',
                        iconCls: 'remove',
                        disabled: true,
                        handler: function(){
                            var selection = grid.getView().getSelectionModel().getSelection()[0];
                            if (selection) {
                                store.remove(selection);
                            }
                        }
                    }]
                }],
                viewConfig: {
                    stripeRows: true,
//                    listeners: {
//                        itemcontextmenu: function(view, rec, node, index, e) {
//                            e.stopEvent();
//                            contextMenu.showAt(e.getXY());
//                            return false;
//                        }
//                    }
                },                
    contextMenu : null,

    listeners: {
        itemclick: function (v, r) {
            r.isExpanded() ? r.collapse() : r.expand();
        },
        itemcontextmenu: function(view, rec, node, index, e) {
            var me = this, menu = me.contextMenu;
            e.stopEvent();
            menu.showAt(e.getXY());
            menu.doConstrain();
        }
    },

    initComponent: function () {
        var me = this;
//        var testAction = Ext.create('Ext.Action', {
//            icon   : '../ext/examples/shared/icons/fam/delete.gif',  // Use a URL in the icon config
//            text: 'Test',
//            disabled: true,
//            handler: function(widget, event) {
//                var selection = me.getView().getSelectionModel().getSelection()[0];
//                selection.data.ntname='xxxx';
//                me.store.save(selection);
//                console.log(selection.data.id);
//                console.log(selection.data.ntname);
//            }
//        });
//
//        me.contextMenu = Ext.create('Ext.menu.Menu', {
//            items: [ testAction ]
//        });            
//
//        me.getSelectionModel().on('selectionchange', function(selModel, selections){
//            if (selections.length) {
//                testAction.enable();
//            } else {
//                testAction.disable();
//            }                
//        });

            //var rowEditing = Ext.create('Ext.grid.plugin.RowEditing');

            var delAction = Ext.create('Ext.Action', {
                icon   : '../ext/examples/shared/icons/fam/delete.gif',  // Use a URL in the icon config
                text: 'Delete NT',
                disabled: true,
                handler: function(widget, event) {
                    var selection = grid.getView().getSelectionModel().getSelection()[0];
                    if (selection) {
                        store.remove(selection);
                    }
                }
            });
            var testAction = Ext.create('Ext.Action', {
                icon   : '../ext/examples/shared/icons/fam/delete.gif',  // Use a URL in the icon config
                text: 'Test',
                disabled: true,
                handler: function(widget, event) {
                    var selection = grid.getView().getSelectionModel().getSelection()[0];
                    //selection.data.ntname='xxxx';
                    store.save(selection);
                    console.log(selection.valueOf());

                    var win = MyDesktop.GridWindow.getEditWindow();
                    win.show();
                }
            });


            me.contextMenu = Ext.create('Ext.menu.Menu', {
                items: [
                    delAction,
                    testAction
                ]
            });            

            me.getSelectionModel().on('selectionchange', function(selModel, selections){
                me.down('#delete').setDisabled(selections.length === 0);
                if (selections.length) {
                    delAction.enable();
                    testAction.enable();
                } else {
                    delAction.disable();
                    testAction.disable();
                }                
            });
        
        me.callParent();
    },
 });
